<?php

namespace app\index\controller;
use app\common\model\activity\SalesActivity as SalesActivityModel;
use app\common\model\activity\SalesActivityChannel as SalesActivityChannelModel;
use app\common\model\activity\SalesChannel as SalesChannelModel;
use app\common\model\activity\Coupons as CouponsModel;
use app\common\model\oms\StoreAuth as StoreAuthModel;
use app\common\model\activity\CouponsStore as CouponsStoreModel;
use app\common\model\oms\Store as StoreModel;
use think\Validate;

class Salesactivity extends \app\common\controller\AuthBase {

    public function __construct() {
        parent::__construct();
    }

    
    /**
     * 获取营销活动信息
     * @param int $id
     * @return array $data(返回的营销活动信息)
     * @Author:qinlingguang
     * @Time:2016-04-12
     */
    public function ActivityInfo() {

        $params = $this->request->param();
        $rule = [
            "channel_id|渠道id" => 'require|number|max:11',
            "id|活动id"=>'require|number|max:11'
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $salesActivityModel = new SalesActivityModel();
        $salesActivity = $salesActivityModel->getOne(['id'=>$params['id']]);
        
        //将查询出来的数据转化成为数组
        $salesActivity = $this->TransModelArr($salesActivity);
        if(empty($salesActivity)){
            return $this->output([], 'sales_activity is empty,sales_activity id:'.$params['id']);
        }
        
        $salesChannelModel = new SalesChannelModel();
        $salesChannel = $salesChannelModel->getOne(['id'=>$params['channel_id']]);

        if (!$salesChannel) {
            return $this->output([],'sales_channel is empty,sales_channel id:'.$params['channel_id']);
        }
        
        //渠道状态录入
        $salesActivity['c_status'] = $salesChannel['status'];
        //获得活动对应的渠道状态
        $salesActivityChannelModel = new SalesActivityChannelModel();
        
        $salesActivityChannel = $salesActivityChannelModel->getOne(['activity_id'=>$params['id'],'channel_id'=>$params['channel_id']]);
        if (!$salesActivityChannel) {
            return $this->output([], "sales_activity_channel is empty,activity_id:{$params['id']},channel_id:{$params['channel_id']}");
        }
        //活动对应的渠道状态录入
        $salesActivity['channel_status'] = $salesActivityChannel['status'];
        return $this->output($salesActivity, 'success');
    }
    
    /**
     * 获取优惠券使用门店列表
     * @param type $coordinate(经纬度坐标)
     * @param type $couponsId （优惠券Id）
     * @param type $pageCondition (分页条件)
     * @param type $orderBy   （排序条件）
     * @Author:qinlingguang
     * @Time:2016-04-13
     */
    public function couponsStore() {
        
        $params = $this->input();
        $rule = [
            "coordinate|经纬度坐标" => 'require',//经纬度按逗号分隔的字符串
            "coupons_id|优惠券id"=>'require|number|max:11',
            'page|页码'=>'require|number',
            'pageSize|每页多少'=>'require|number|between:2,200',
            'order_by|排序'=>'max:50',
            'city_id|城市Id'=>'max:50',
            'region_id|地区Id'=>'max:100'
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $params['order_by']  = isset($params['order_by']) && !empty($params['order_by'])?$params['order_by']:'distance ASC';
        $params['city_id']   = isset($params['city_id']) && !empty($params['city_id'])?$params['city_id']:'';
        $params['region_id'] = isset($params['region_id']) && !empty($params['region_id'])?$params['region_id']:'';
        
        $couponsModel = new CouponsModel();
        //根据券的id查出券的信息
        $couponsInfo = $couponsModel->getOne(['id'=>$params['coupons_id']]);
        $storeAuthModel = new StoreAuthModel();
        $couponsStoreModel = new CouponsStoreModel();

        //判断类型，根据不同的type查出门店数据，1适用全部  2.适用自己的店  3.适用授权的店  4.适用指定门店
        if($couponsInfo['store_type'] == 4){
            $list = $couponsStoreModel->getStoreArrByCouponsId($params['coupons_id']);
        }elseif($couponsInfo['store_type'] == 1 || $couponsInfo['store_type'] == 2 || $couponsInfo['store_type'] == 3){
            $list = $storeAuthModel->getStoreList($couponsInfo['store_type'],$couponsInfo['company_id']);
        }else{
            return $this->output([], "the coupon applies to the wrong type of store_type:{$couponsInfo['store_type']}");
        }
        //检查优惠券是否存在使用门店
        if(!$list){
            return $this->output([], "coupon does not exist for applicable stores,coupon_id:{$params['coupons_id']}");
        }
        
        $storeModel = new StoreModel();
        $data = $storeModel->getCouponsStore($params['coordinate'], $list, $params['page'],$params['pageSize'], $params['order_by'],  $params['city_id'], $params['region_id']);
        //判断适用门店是否存在
        if(!$data){
            return $this->output([], 'coupon does not exist,coupon_id:'.$params['coupons_id']);
        }
        return $this->output($data,'success');
    }
    
    /**
     * 获取叽叽券活动列表
     * @param type $params
     * @return type
     */
    public function getList() {

        $params = $this->request->param();
        $rule = [
            "company_id|公司id" => 'require|number',
            "keywords|活动名称" => 'max:100',
            "status|状态" => 'number',
            "page|页码" => 'number',
            "pageSize|每页数量" => 'number',
            "order|排序" => 'max:100',
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $SalesActivityModel = new SalesActivityModel();
        $data = $SalesActivityModel->getList($params);
        
        if($data === false){
            return $this->output([], 'company_id is error');
        }
        return $this->output($data, 'success');
    }
    
    
    /**
     * 更新叽叽券活动接口
     * @param type $activityId
     * @param type $idata
     * @return type
     */
    public function update() {

        $params = $this->request->param();
        $rule = [
            "status|要修改成何种状态" => 'between:0,6',
            "activity_id|活动id" => 'require|number',
            "activity_name|活动名称"=>'max:24',
            "end_date|结束日期"=>'number',
            "activity_rule|活动规则"=>'max:600',
            "activity_img|活动图片"=>'max:256',
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $where = [
            'status'=>isset($params['status'])?$params['status']:'',
            'activity_name'=>isset($params['activity_name'])?$params['activity_name']:'',
            'end_date'=>isset($params['end_date'])?$params['end_date']:'',
            'activity_rule'=>isset($params['activity_rule'])?$params['activity_rule']:'',
            'activity_img'=>isset($params['activity_img'])?$params['activity_img']:'',
        ];
        
        $where = array_filter($where);
        
        $SalesActivityModel = new SalesActivityModel();
        $data = $SalesActivityModel->updateData($params['activity_id'], $where);
        
        return $this->output([], 'success');
    }
    
    
    /**
     * 
     * @param type $data
     * @return boolean
     */
    public function add() {
        
        //注意严格过滤所以是post而不是param
        $params = $this->request->post();
        if (!is_array($params)) {
            $this->output([], 'params format is error');
        }
        foreach ($params as $key => $value) {
            if (!in_array($key, ['activity_type', 'activity_name', 'start_date', 'end_date', 'activity_rule', 'activity_img', 'status', 'coupons_id', 'company_id'])) {
                $this->output([], "params is illegal ({$key})");
            }
        }
        
        $SalesActivityModel = new SalesActivityModel();
        //检测activity_name有没有重复添加
        $record = $SalesActivityModel->getOne(['activity_name'=>$params['activity_name']]);
        if(!empty($record)){
            return $this->output([], 'activity_name already have');
        }
        $id = $SalesActivityModel->addData($params);
        
        if($id){
            return $this->output([], 'success');
        }
        
        return $this->output([], 'fail');
    }
    
    
    /**
     * 获取活动详情
     * @param type $activityId
     */
    public function getInfo() {
        
        $params = $this->request->param();
        $rule = [
            "activity_id|活动id" => 'require|number',
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $SalesActivityModel = new SalesActivityModel();
        $SalesActivity = $SalesActivityModel->getOne(['id' => $params['activity_id']]);
        
        return $this->output($SalesActivity, 'success');
    }
    
    
    /**
     * 获取渠道列表
     * @param type $params
     * @return type
     */
    public function getChannelList() {

        $params = $this->request->param();
        $rule = [
            "company_id|公司id" => 'require|number',
            "page|页码" => 'number',
            "keywords|关键字" => 'max:34',
            "pageSize|每页多少"=>'number',
            "order|排序字符串"=>'max:100',
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $salesChannelModel = new SalesChannelModel();
        $data = $salesChannelModel->getList($params);
        if($data === false){
            return $this->output([], 'company_id is error');
        }
        return $this->output($data, 'success');
    }
    
    /**
     * 获取渠道详情
     * @param type $channelId
     * @return type
     */
    public function getChannelInfo() {

        $params = $this->request->param();
        $rule = [
            "channel_id|渠道id" => 'require|number',
        ];
        
        $validate = new Validate($rule);
        if(!$validate->check($params)){
            return $this->output([], $validate->getError());
        }
        
        $salesChannelModel = new SalesChannelModel();
        $result = $salesChannelModel->getChannelInfo($params['channel_id']);
        return $this->output($result, 'success');
    }
    
    
   /**
     * 开启/关闭渠道
     * @param type $channelId
     * @return type
     */
   public function switchsChannel() {

       $params = $this->request->param();
       $rule = [
           "channel_id|渠道id" => 'require|number',
           "status|状态" => 'require|between:0,1'
       ];
        
       $validate = new Validate($rule);
       if(!$validate->check($params)){
            return $this->output([], $validate->getError());
       }
       
       $SalesChannelModel = new SalesChannelModel();
       $SalesChannelModel->switchChannel($params['channel_id'], $params['status']);
       
       return $this->output([], 'success');
    }
    
    /**
      * 添加渠道
      */
    public function addChannel() {

       $params = $this->request->param();
       $rule = [
           "channel_name|渠道名称" => 'require|max:34',
           "channel_describe|渠道描述" => 'require|max:34',
           "channel_type|合作类型"=>'require|number|between:1,1',
           "add_date|添加时间"=>'require|number',
           "update_date|更新时间"=>'require|number',
           "company_id|公司Id"=>'require|number',
           "status|状态"=>'require|number',
       ];
        
       $validate = new Validate($rule);
       if(!$validate->check($params)){
            return $this->output([], $validate->getError());
       }
        
        $salesChannelModel = new SalesChannelModel();
        $result = $salesChannelModel->getOne(['channel_name' => $params['channel_name'],'company_id'=> $params['company_id']]);
        if(!empty($result)){
            return $this->output([], 'channel_name is have in this company_id');
        }
        
        $iData = [
            'channel_type'=>isset($params['channel_type'])?$params['channel_type']:'',
            'channel_name'=>isset($params['channel_name'])?$params['channel_name']:'',
            'channel_describe'=>isset($params['channel_describe'])?$params['channel_describe']:'',
            'add_date'=>isset($params['add_date'])?$params['add_date']:'',
            'update_date'=>isset($params['update_date'])?$params['update_date']:'',
            'company_id'=>isset($params['company_id'])?$params['company_id']:'',
            'status'=>isset($params['status'])?$params['status']:'',
        ];
        
        $salesChannelModel->addChannel(array_filter($iData));
        return $this->output([], 'success');
    }
    
    
    /**
     * 获取已投放渠道列表
     * @param type $params
     * @return type
     */
    public function servedActivity() {

        $params = $this->request->param();
        $rule = [
            "company_id|公司id" => 'require|max:11',
            "activity_id|活动id" => 'require|max:11',
            "page|页码" => 'number',
            "pageSize|每页多少"=>'number|between:1,200'
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
             return $this->output([], $validate->getError());
        }
        
        $salesActivityModel = new SalesActivityModel();
        $data = $salesActivityModel->servedActivity($params);
        return $this->output($data, 'success');
    }
    
    
    /**
     * 获取未投放渠道列表
     * @return type
     */
    public function notRunningActivity() {

        $params = $this->request->param();
        $rule = [
            "company_id|公司id" => 'require|max:11',
            "activity_id|活动id" => 'require|max:11',
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
             return $this->output([], $validate->getError());
        }
        
        $salesActivityChannelModel = new SalesActivityChannelModel();
        $result = $salesActivityChannelModel->notRunningActivity($params);

        return $this->output($result, 'success');
    }
    
    /**
     * 将活动关联渠道
     * @return type
     */
    public function activityWithChannel() {
        $params = $this->request->param();
        $rule = [
            "company_id|公司id" => 'require|number|max:11',
            "activity_id|活动id" => 'require|number|max:11',
            "channel_id|渠道id"=>'require|number|max:11'
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
             return $this->output([], $validate->getError());
        }
        
        $salesActivityChannelModel = new SalesActivityChannelModel();
        $salesActivityChannel = $salesActivityChannelModel->activityWithChannel(
                $params['company_id'],$params['activity_id'], $params['channel_id']);

        return $this->output($salesActivityChannel, 'success');        
    }
    
    
    /**
     * 关闭渠道
     * @param type $id
     * @return type
     */
    public function channelStatusSwitch() {
        
        $params = $this->request->param();
        $rule = [
            "id|销售活动渠道id" => 'require|number|max:11',
            "status|状态" => 'require|number|max:11',
        ];

        $validate = new Validate($rule);
        if(!$validate->check($params)){
             return $this->output([], $validate->getError());
        }

        $salesActivityChannelModel = new SalesActivityChannelModel();
        $result = $salesActivityChannelModel->switchStatus($params['id'],$params['status']);
        return $this->output($result, 'success');
    }
}
